/*
 * @Description: 生成Controller
 * @Author: ${codeGenConfig.genAuthor}
 * @Date: 2022-01-30 14:20:12
 * @LastEditors: DHL
 * @LastEditTime: 2022-07-20 15:46:40
 */

import { firstToUpperCase, firstToLowerCase } from '../../utils/codeGenUtils'

export function createController(codeGenConfig: any) {
  const classNameFirstToUpperCase = firstToUpperCase(codeGenConfig.className)
  const classNameFirstToLowerCase = firstToLowerCase(codeGenConfig.className)
  const variableService = classNameFirstToLowerCase + 'Service'
  const classNameServiceInterface = 'I' + codeGenConfig.className + 'Service'
  const className = codeGenConfig.className + 'Controller'

  return `
package ${codeGenConfig.packageName}.controller;

import java.util.List;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import ${codeGenConfig.packageName}.entity.${classNameFirstToLowerCase}.${classNameFirstToUpperCase};
import ${codeGenConfig.packageName}.entity.${classNameFirstToLowerCase}.dto.Get${classNameFirstToUpperCase}ByIdDto;
import ${codeGenConfig.packageName}.entity.${classNameFirstToLowerCase}.dto.Search${classNameFirstToUpperCase}Dto;
import ${codeGenConfig.packageName}.entity.${classNameFirstToLowerCase}.vo.Save${classNameFirstToUpperCase}Vo;
import ${codeGenConfig.packageName}.entity.${classNameFirstToLowerCase}.vo.Search${classNameFirstToUpperCase}Vo;
import ${codeGenConfig.packageName}.entity.${classNameFirstToLowerCase}.vo.Update${classNameFirstToUpperCase}ByIdVo;
import ${codeGenConfig.packageName}.service.${classNameServiceInterface};
import com.twb.core.base.Result;
import com.twb.core.base.controller.BaseController;
import com.twb.core.base.entity.TwbPageInfo;
import com.twb.core.security.annotation.Auth;
import com.twb.core.util.BeanUtils;
import com.twb.core.util.Results;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @ClassName: ${className}
 * @Description: ${codeGenConfig.genName}Controller
 * 
 * @author ${codeGenConfig.genAuthor}
 * @date ${codeGenConfig.createData}
 */
@RestController
@RequestMapping("/${classNameFirstToLowerCase}")
@Api(value = "${className}", tags = "${codeGenConfig.genName}")
public class ${className} extends BaseController {
  @Autowired
  ${classNameServiceInterface} ${variableService};

  /**
   * @Title: search
   * @Description: 分页查询
   * @param vo
   * @return
   * @author ${codeGenConfig.genAuthor}
   * @date ${codeGenConfig.createData}
   */
  @GetMapping("/search")
  @ApiOperation(value = "分页查询")
  public Result<TwbPageInfo<Search${classNameFirstToUpperCase}Dto>> search(Search${classNameFirstToUpperCase}Vo vo) {
    TwbPageInfo<Search${classNameFirstToUpperCase}Dto> result = ${variableService}.search${classNameFirstToUpperCase}(vo);
    return Results.successWithData(result);
  }

  /**
   * @Title: searchSum
   * @Description: 列表查询合计
   * @param vo
   * @return
   * @author ${codeGenConfig.genAuthor}
   * @date ${codeGenConfig.createData}
   */
  @Auth(verifyURL = false)
  @GetMapping("/searchSum")
  @ApiOperation(value = "列表查询合计")
  public Result<Search${classNameFirstToUpperCase}Dto> searchSum(Search${classNameFirstToUpperCase}Vo vo) {
    Search${classNameFirstToUpperCase}Dto result = ${variableService}.search${classNameFirstToUpperCase}Sum(vo);
    return Results.successWithData(result);
  }

  /**
   * @Title: getById
   * @Description: 根据ID获取详情
   * @param id
   * @return
   * @author ${codeGenConfig.genAuthor}
   * @date ${codeGenConfig.createData}
   */
  @GetMapping("/getById")
  @ApiOperation(value = "根据ID获取详情")
  public Result<Get${classNameFirstToUpperCase}ByIdDto> getById(
    @NotBlank 
    @RequestParam 
    @ApiParam(value = "主键", name = "id", required = true) 
    String id
  ) {
    ${classNameFirstToUpperCase} ${classNameFirstToLowerCase} = ${variableService}.getById(id);
    if (${classNameFirstToLowerCase} == null) {
      return Results.failure("数据不存在");
    }
    Get${classNameFirstToUpperCase}ByIdDto dto = new Get${classNameFirstToUpperCase}ByIdDto();
    BeanUtils.copyNotNullProperties(${classNameFirstToLowerCase}, dto);
    return Results.successWithData(dto);
  }

  /**
   * @Title: save
   * @Description: 保存信息
   * @param vo
   * @return
   * @author ${codeGenConfig.genAuthor}
   * @date ${codeGenConfig.createData}
   */
  @PostMapping("/save")
  @ApiOperation(value = "保存信息")
  public Result<String> save(@RequestBody @Valid Save${classNameFirstToUpperCase}Vo vo) {
    boolean result = ${variableService}.save${classNameFirstToUpperCase}(vo);
    if (result) {
      return Results.success("保存成功");
    } else {
      return Results.failure("保存失败");
    }
  }

  /**
   * @Title: updateById
   * @Description:更新信息
   * @param vo
   * @return
   * @author ${codeGenConfig.genAuthor}
   * @date ${codeGenConfig.createData}
   */
  @PutMapping("/updateById")
  @ApiOperation(value = "更新信息")
  public Result<String> updateById(@RequestBody @Valid Update${classNameFirstToUpperCase}ByIdVo vo) {
    boolean result = ${variableService}.update${classNameFirstToUpperCase}ById(vo);
    if (result) {
      return Results.success("更新成功");
    } else {
      return Results.failure("更新失败");
    }
  }

  /**
   * @Title: deleteBatchByIds
   * @Description:批量删除信息
   * @param ids
   * @return
   * @author ${codeGenConfig.genAuthor}
   * @date ${codeGenConfig.createData}
   */
  // @Auth(verifyToken = true)
  // @PostMapping("/batchDeleteByIds")
  // @ApiOperation(value = "批量删除信息")
  // public Result<String> deleteBatchByIds(
  //   @NotBlank 
  //   @RequestParam 
  //   @ApiParam(value = "主键集合", name = "ids", required = true
  // ) String ids) {
  //   boolean result = ${variableService}.delete${classNameFirstToUpperCase}ByIds(ids);
  //   if (result) {
  //     return Results.success("删除成功");
  //   } else {
  //     return Results.failure("删除失败");
  //   }
  // }

  /**
   * @Title: logicBatchDeleteByIds
   * @Description: 批量逻辑删除信息
   * @param ids
   * @return
   * @author ${codeGenConfig.genAuthor}
   * @date ${codeGenConfig.createData}
   */
  @Auth(verifyToken = true)
  @PostMapping("/logicBatchDeleteByIds")
  @ApiOperation(value = "批量逻辑删除信息")
  public Result<String> logicBatchDeleteByIds(
    @NotBlank 
    @RequestParam 
    @ApiParam(value = "主键集合", name = "ids", required = true
  ) String ids) {
    int result = ${variableService}.logicBatchDelete${classNameFirstToUpperCase}ByIds(ids);
    if (result > 0) {
      return Results.success("删除成功");
    } else {
      return Results.failure("删除失败");
    }
  }


  /**
   * @Title exportExcel
   * @Description 导出Excel
   * @param vo
   * @param response
   * @throws Exception
   * @author ${codeGenConfig.genAuthor}
   * @date ${codeGenConfig.createData}
   */
  @PostMapping("/exportExcel")
  @ApiOperation(value = "导出excel")
  public void exportExcel(@RequestBody Search${classNameFirstToUpperCase}Vo vo, HttpServletResponse response) throws Exception {

    ${variableService}.exportExcel(vo, response);
  }


  /**
   * @Title importExcel
   * @Description 导入Excel
   * @param importId
   * @author ${codeGenConfig.genAuthor}
   * @date ${codeGenConfig.createData}
   */
  @PostMapping("/import")
  @ApiOperation(value = "导入excel")
  public Result<String> importExcel(
    @NotBlank
    @RequestParam
    @ApiParam(value="导入任务主键", name="importId", required=true)
    String importId
  ) {

    ${variableService}.importExcel(importId);

    return Results.success("数据导入中");
  }
}
`
}
